/**
 * 
 */
package cn.ad.server.web.spring.service.mapping;

import cn.ad.server.web.spring.service.bomodel.PlatIndustry;
import cn.ad.server.web.spring.service.ehcache.EHCacheHelper;
import cn.ad.server.web.spring.service.ehcache.EhCacheConstants;
import cn.ad.server.domain.ad.Industry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Wu Chengjun
 */
@Service
public class IndustrayMapperImpl implements IndustryMapper{

	@Autowired
	EHCacheHelper eHCacheHelper;
	
	/*
	 * (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.IndustryMapper#findAdserverIndustry(int, int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Set<Industry> findAdserverIndustry(int platformID, int platformIndustryID) {
		if(platformID <= 0){
			throw new IllegalArgumentException("platformID is invalid.");
		}
		if(platformIndustryID <= 0){
			throw new IllegalArgumentException("platformIndustryID is null");
		}
		PlatIndustry platIndustry = eHCacheHelper.getCache(EhCacheConstants.CACHE_INDUSTRY, new PlatIndustry.Key(platformID, platformIndustryID), PlatIndustry.class);
		if(platIndustry != null){
			return platIndustry.getIndustrys();
		}else{
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.IndustryMapper#findAdserverIndustry(int, int[][])
	 */
	@Override
	public Set<Industry> findAdserverIndustry(int platformID, int[] platformIndustryIDs) {
		if(platformID <= 0){
			throw new IllegalArgumentException("platformID is invalid.");
		}
		if(platformIndustryIDs == null){
			throw new IllegalArgumentException("platformIndustryID is null");
		}
		
		if(platformIndustryIDs.length == 0){
			return null;
		}
		
		Set<Industry> set = new HashSet<Industry>();
		
		for(int piID : platformIndustryIDs){
			Set<Industry> s = findAdserverIndustry(platformID, piID);
			if(s != null){
				set.addAll(s);
			}
		}
		return set;
	}
}
